﻿using System.Collections.Generic;
using System;

namespace TestGenerator
{
    class QuestionSet
    {
        public QuestionSet()
        {
            correctAnswers = new List<string>();
            wrongAnswers = new List<string>();
            rand = new Random();
        }

        public string Question
        {
            get
            {
                return question;
            }
            set
            {
                question = value;
            }
        }

        public void AddAnswer(string answer, bool correct)
        {
            if (correct)
            {
                correctAnswers.Add(answer);
            }
            else
            {
                wrongAnswers.Add(answer);
            }
        }

        public RealQuestion GetRandomQuestion()
        {

            if (wrongAnswers.Count < 1)
            {
                throw new ArgumentException("Not enough wrong answers");
            }

            if (correctAnswers.Count < 1)
            {
                throw new ArgumentException("Not enough correct answers");
            }

            List<string> randomAnswers = new List<string>();
            int answersCount = Math.Min(Settings.AnswersCount, wrongAnswers.Count + 1);
            int correctAnswerNumber = rand.Next(answersCount);
            HashSet<int> usedWrongAnswers = new HashSet<int>();

            for (int i = 0; i < answersCount; i++)
            {
                if (i == correctAnswerNumber)
                {
                    randomAnswers.Add(correctAnswers[rand.Next(correctAnswers.Count)]);
                }
                else
                {
                    int r;
                    do
                    {
                        r = rand.Next(wrongAnswers.Count);
                    }
                    while (usedWrongAnswers.Contains(r));
                    usedWrongAnswers.Add(r);

                    randomAnswers.Add(wrongAnswers[r]);
                }
            }

            RealQuestion realQuestion = new RealQuestion(Question, randomAnswers, correctAnswerNumber);
            return realQuestion;
        }

        private string question;
        private List<string> correctAnswers;
        private List<string> wrongAnswers;
        private Random rand;
    }

    class RealQuestion
    {
        public RealQuestion(string question, List<string> answers, int correctAnswer)
        {
            this.question = question;
            this.answers = answers;
            this.correctAnswer = correctAnswer;
        }

        public string Question
        {
            get
            {
                return question;
            }
        }

        public List<string> Answers
        {
            get
            {
                return answers;
            }
        }

        public int CorrectAnswer
        {
            get
            {
                return correctAnswer;
            }
        }

        private string question;
        private List<string> answers;
        private int correctAnswer;
    }
}